#--*--encoding:utf-8--*--

# MySQL python connector required.
# http://dev.mysql.com/downloads/connector/python/

import mysql.connector

DB_HOST = 'localhost'
DB_NAME = 'xdd'
DB_USER = 'root'
DB_PASSWORD = '123465'

conn = mysql.connector.connect(user=DB_USER, password=DB_PASSWORD, host=DB_HOST, database=DB_NAME)
cursor = conn.cursor()

conn2 = mysql.connector.connect(user=DB_USER, password=DB_PASSWORD, host=DB_HOST, database=DB_NAME)
cursor2 = conn2.cursor()

codes = '''
- (void)loadCategoryData {
	//课程区域
	_areaDatas = [NSMutableArray array];
	[_areaDatas addObject:@{@"title":@"不限", @"id":@"0"}];
'''

# 课程区域
sql = 'SELECT id, name FROM sys_area WHERE id < 41'

cursor.execute(sql)
for (id, name) in cursor:
	line = '\t[_areaDatas addObject:@{@"title":@"%s", @"id":@"%s"}];\n' % (name, id)
	codes = codes + line

codes = codes + '''
    //文化课数据
	_cultureCategoryDatas = [NSMutableArray array];
	_cultureCategoryDatas = [NSMutableArray array];
    _cultureCategory1Datas = [NSMutableArray array];
    _cultureCategory2Datas = [NSMutableArray array];
    _cultureCategory3Datas = [NSMutableArray array];

'''

# 文化课一级
sql = 'SELECT id, name FROM course_search_criteria_level_one WHERE type=0 ORDER BY ord'

clevel_one_arr = []

cursor.execute(sql)
for (id, name) in cursor:
	clevel_one_arr.append({'id': id, 'name': name})

#文化课二级
for clevel_one in clevel_one_arr:
	line = '\t[_cultureCategoryDatas addObject:@{@"title":@"%s", @"id":@"%s", @"childs":@[' % (clevel_one['name'], clevel_one['id'])

	clevel_two_arr = []
	clevel_two_content = ''

	sql = 'SELECT id, name FROM course_search_criteria_level_two WHERE level_one_id = %s ORDER BY id, ord' % clevel_one['id']
	cursor.execute(sql)
	for (id, name) in cursor:
		clevel_two_arr.append({'id': id, 'name': name})

		temp = '\t\t@{@"title":@"%s", @"id":@"%s", @"childs":@[' % (name, id)

		#文化课三级
		clevel_three_arr = []
		clevel_three_content = ''

		sql = 'SELECT id, name FROM course_search_criteria_level_three WHERE level_two_id = %s ORDER BY id, ord' % id

		cursor2.execute(sql)
		for (id2, name2) in cursor2:
			clevel_three_arr.append({'id': id2, 'name': name2})
			temp2 = '\t\t\t@{@"title":@"%s", @"id":@"%s", @"childs":@[]},\n' % (name2, id2)
			clevel_three_content = clevel_three_content + temp2

		if len(clevel_three_arr) == 0:
			temp = temp + ']},\n'
		else:
			temp = temp + '\n' + clevel_three_content[:clevel_three_content.rfind(',')] + '\n'
			temp = temp + '\t\t]},\n'

		clevel_two_content = clevel_two_content + temp

	if len(clevel_two_arr) == 0:
		line = line + ']}];\n'
	else:
		line = line + '\n' + clevel_two_content[:clevel_two_content.rfind(',')] + '\n'
		line = line + '\t]}];\n'

	codes = codes + line



codes = codes + '''
	[_cultureCategory1Datas addObjectsFromArray:_cultureCategoryDatas];


    //艺体类数据
	_artCategoryDatas = [NSMutableArray array];
    _artCategory1Datas = [NSMutableArray array];
    _artCategory2Datas = [NSMutableArray array];
    _artCategory3Datas = [NSMutableArray array];
'''

# 艺体类一级
sql = 'SELECT id, name FROM course_search_criteria_level_one WHERE type=1 ORDER BY ord'

clevel_one_arr = []

cursor.execute(sql)
for (id, name) in cursor:
	clevel_one_arr.append({'id': id, 'name': name})

#艺体类二级
for clevel_one in clevel_one_arr:
	line = '\t[_artCategoryDatas addObject:@{@"title":@"%s", @"id":@"%s", @"childs":@[' % (clevel_one['name'], clevel_one['id'])

	clevel_two_arr = []
	clevel_two_content = ''

	sql = 'SELECT id, name FROM course_search_criteria_level_two WHERE level_one_id = %s ORDER BY id, ord' % clevel_one['id']
	cursor.execute(sql)
	for (id, name) in cursor:
		clevel_two_arr.append({'id': id, 'name': name})

		temp = '\t\t@{@"title":@"%s", @"id":@"%s", @"childs":@[' % (name, id)

		#艺体类三级
		clevel_three_arr = []
		clevel_three_content = ''

		sql = 'SELECT id, name FROM course_search_criteria_level_three WHERE level_two_id = %s ORDER BY id, ord' % id

		cursor2.execute(sql)
		for (id2, name2) in cursor2:
			clevel_three_arr.append({'id': id2, 'name': name2})
			temp2 = '\t\t\t@{@"title":@"%s", @"id":@"%s", @"childs":@[]},\n' % (name2, id2)
			clevel_three_content = clevel_three_content + temp2

		if len(clevel_three_arr) == 0:
			temp = temp + ']},\n'
		else:
			temp = temp + '\n' + clevel_three_content[:clevel_three_content.rfind(',')] + '\n'
			temp = temp + '\t\t]},\n'

		clevel_two_content = clevel_two_content + temp

	if len(clevel_two_arr) == 0:
		line = line + ']}];\n'
	else:
		line = line + '\n' + clevel_two_content[:clevel_two_content.rfind(',')] + '\n'
		line = line + '\t]}];\n'

	codes = codes + line


codes = codes + '\n\t[_artCategory1Datas addObjectsFromArray:_artCategoryDatas];\n'
codes = codes + '}'

fp = open('CourseData.m', 'w', encoding='utf-8')
fp.write(codes)
fp.close()

cursor.close()
conn.close()

cursor2.close()
conn2.close()